package com.ll.daipao.controller;

import com.alibaba.fastjson.JSONObject;
import com.ll.daipao.dao.TAccountMapper;
import com.ll.daipao.handler.Constants;
import com.ll.daipao.service.OrderService;
import com.ll.daipao.vo.TAccount;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by ll on 2017/10/24.
 */
@Controller
@RequestMapping("myhome")
public class MyHomeController extends BaseController{
   @Autowired
   TAccountMapper tAccountMapper;
   @Autowired
   OrderService orderService;

    @RequestMapping("index")
    public String index(HttpServletRequest request, Model model){
        String openId = (String) request.getSession().getAttribute(Constants.WX_OPENID);
        JSONObject myhome = orderService.myhome(openId);
        model.addAllAttributes(myhome);
        return "personCenter";
    }

    @RequestMapping("mineSchool")
    public String mineSchool(HttpServletRequest request, Model model){
        return "mineSchool";
    }


    @RequestMapping("saveSchool")
    @ResponseBody
    public String saveSchool(HttpServletRequest request, Model model){
        String opendid= (String) request.getSession().getAttribute(Constants.WX_OPENID);
        String id = request.getParameter("id");
        if(StringUtils.isBlank(id)){
            return failed("缺少重要数据!");
        }

        JSONObject res = orderService.saveSchool(opendid, id);
        TAccount tAccount = tAccountMapper.selectAccountByOpenId(opendid);
        request.getSession().setAttribute(Constants.WX_ACCOUNT,tAccount);
        if(res.getBoolean("suc")){
            return success();
        }
        return failed(res.getString("msg"));
    }
}
